package com.zizo.bbs;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.zizo.util.Util;

public class BBS implements Runnable {
	private int id;

	public BBS(int id) {
		super();
		this.id = id;
	}

	@Override
	public void run() {
		String url = "http://club.autohome.com.cn/bbs/thread-c-492-" + id + "-1.html";
		// http://club.autohome.com.cn/bbs/thread-c-492-706371-1.html
		RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000) // socket超时
				.setConnectTimeout(20000) // connect超时
				.build();
		CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();

		try {
			// 创建httpget
			HttpGet httpget = new HttpGet(url);
			config(httpget);
			// 执行get请求
			CloseableHttpResponse response = httpClient.execute(httpget);
			try {
				// 获取响应实体
				HttpEntity entity = response.getEntity();
				// 响应状态
				if (entity != null) {
					// 响应内容
					String data = EntityUtils.toString(entity);
					if (data.contains("对不起，您访问的主题不存在") || data.contains("403 Forbidden")
							|| data.contains("帖子被删除（或屏蔽）！")) {
						// System.out.println("ERROR ID:" + this.id);
						return;
					} else {
						Util.downloadWebsite("/mnt/mirror/server/server/autohome/bbs/" + this.id / 10000,
								this.id + ".html", data);
						Util.addFileToZip(
								"/mnt/mirror/server/server/autohome/bbs/" + this.id / 10000 + "/" + this.id + ".html",
								"/mnt/mirror/server/server/autohome/bbs/" + this.id / 10000 + "/" + this.id + ".zip");
						if (id % 10000 == 0) {
							System.out.println(this.id + "\t" + Util.getCurrentDateTime());
						}
					}
				}
			} finally {
				response.close();
			}
		} catch (HttpHostConnectException e) {
			System.out.println("HTTP ERROR:" + this.id);
		} catch (ConnectTimeoutException conne) {
		} catch (UnknownHostException une) {
		} catch (SocketTimeoutException e) {
			System.out.println("ReadTimeOut:" + this.id);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println(url);
		} finally {
			// 关闭链接,释放资源
			try {
				httpClient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private static void config(HttpRequestBase httpRequestBase) {
		// 设置Header等
		httpRequestBase.setHeader("User-Agent",
				"User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1;Trident/5.0;");
		httpRequestBase.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		httpRequestBase.setHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");// "en-US,en;q=0.5");
		httpRequestBase.setHeader("Accept-Charset", "ISO-8859-1,utf-8,gbk,gb2312;q=0.7,*;q=0.7");
		// 配置请求的超时设置
		RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(10000).setConnectTimeout(20000)
				.setSocketTimeout(20000).build();
		httpRequestBase.setConfig(requestConfig);
	}

}
